'use strict';
const db=uniCloud.database();
exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log('event : ', event)
	const id = event.id || ''
	// 插入数据时设置创建时间
	const date = new Date();
	const createTime = date.getTime();
	const table = event.table
	let data={
		title:event.title,
		content:event.content,
	}
		//添加数据到云数据库
		let result=''
		if(!id){ // 添加
			console.log('add ')
			data['create_time'] = createTime
			result = await db.collection(table).add(data)
		}else{  // 修改
			console.log('edit ')
			data['update_time'] = createTime
			result = await db.collection(table).doc(id).update(data)
		}
		if(result){
			return {
				code:200,
				msg:"提交完成"
			}
		}else{
			return {
				code:501,
				msg:"提交失败"
			}
		}
	//返回数据给客户端
	return event
};
